<?php

class wgReloadTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // $this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));

  	$this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', 'frontend'),
      new sfCommandOption('with-images', 'i', sfCommandOption::PARAMETER_NONE, 'Do generate images'),
    ));
  	
    $this->namespace        = 'wg';
    $this->name             = 'reload';
    $this->briefDescription = '';
    $this->detailedDescription = <<<EOF
The [wg:load-data|INFO] task does things.
Call it with:

  [php symfony wg:load-data|INFO]
EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {
  	if ($options['with-images']) {
	    @sfToolkit::clearDirectory(sfConfig::get('sf_upload_dir'));
	    $this->dircopy(sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.'uploads-fixtures', sfConfig::get('sf_upload_dir'));
  	} else {
  		sfConfig::set('app_skip_image_building', true);
  	}
    
    $insertSql = new sfPropelInsertSqlTask($this->dispatcher, $this->formatter);
  	$insertSql->setCommandApplication($this->commandApplication);
  	$ret = $insertSql->run(array(), array(
  		'--no-confirmation'
  	));
  	
  	if ($ret != 0) return;
  	
  	$loadData = new sfPropelLoadDataTask($this->dispatcher, $this->formatter);
    $loadData->setCommandApplication($this->commandApplication);
    $ret = $loadData->run(array(), array());
    
    if ($ret != 0) return;
    
    $cc = new sfCacheClearTask($this->dispatcher, $this->formatter);
    $cc->setCommandApplication($this->commandApplication);
    $ret = $cc->run(array(), array());
  }
  
  public function dircopy($srcdir, $dstdir, $offset = '', $verbose = false)
{
	// A function to copy files from one directory to another one, including subdirectories and
	// nonexisting or newer files. Function returns number of files copied.
	// This function is PHP implementation of Windows xcopy  A:\dir1\* B:\dir2 /D /E /F /H /R /Y
	// Syntaxis: [$returnstring =] dircopy($sourcedirectory, $destinationdirectory [, $offset] [, $verbose]);
	// Example: $num = dircopy('A:\dir1', 'B:\dir2', 1);

	// Original by SkyEye.  Remake by AngelKiha.
	// Linux compatibility by marajax.
	// Offset count added for the possibilty that it somehow miscounts your files.  This is NOT required.
	// Remake returns an explodable string with comma differentiables, in the order of:
	// Number copied files, Number of files which failed to copy, Total size (in bytes) of the copied files,
	// and the files which fail to copy.  Example: 5,2,150000,\SOMEPATH\SOMEFILE.EXT|\SOMEPATH\SOMEOTHERFILE.EXT
	// If you feel adventurous, or have an error reporting system that can log the failed copy files, they can be
	// exploded using the | differentiable, after exploding the result string.
	//
	if(!isset($offset)) $offset=0;
	$num = 0;
	$fail = 0;
	$sizetotal = 0;
	$fifail = '';
	if(!is_dir($dstdir)) mkdir($dstdir);
	if($curdir = opendir($srcdir)) {
		while($file = readdir($curdir)) {
			if($file[0] != '.') {
//				$srcfile = $srcdir . '\\' . $file;	# deleted by marajax
//				$dstfile = $dstdir . '\\' . $file;	# deleted by marajax
				$srcfile = $srcdir . '/' . $file;	# added by marajax
				$dstfile = $dstdir . '/' . $file;	# added by marajax
				if(is_file($srcfile)) {
					if(is_file($dstfile)) $ow = filemtime($srcfile) - filemtime($dstfile); else $ow = 1;
					if($ow > 0 && strpos($srcfile, '.svn') === false) {
						if($verbose) echo "Copying '$srcfile' to '$dstfile'...<br />";
						if(copy($srcfile, $dstfile)) {
							touch($dstfile, filemtime($srcfile)); $num++;
							chmod($dstfile, 0777);	# added by marajax
							$sizetotal = ($sizetotal + filesize($dstfile));
							if($verbose) echo "OK\n";
						} else {
							echo "Error: File '$srcfile' could not be copied!<br />\n";
							$fail++;
							$fifail = $fifail.$srcfile.'|';
						}
					}
				}
				else if(is_dir($srcfile)) {
//					$res = explode(',',$ret);
//					$ret = $this->dircopy($srcfile, $dstfile, $verbose);
$this->dircopy($srcfile, $dstfile, $verbose);
//					$mod = explode(',',$ret);
//					$imp = array($res[0] + $mod[0],$mod[1] + $res[1],$mod[2] + $res[2],$mod[3].$res[3]);
//					$ret = implode(',',$imp);
				}
			}
		}
		closedir($curdir);
	}
//	$red = explode(',',$ret);
//	$ret = ($num + $red[0]).','.(($fail-$offset) + $red[1]).','.($sizetotal + $red[2]).','.$fifail.$red[3];
//	return $ret;
}
}
